bitkeeper revision 1.286 (3f0982812_NP4IcqTvtRwEbA4TDhUQ)
authorrac61@labyrinth.cl.cam.ac.uk <rac61@labyrinth.cl.cam.ac.uk>
Mon, 7 Jul 2003 14:24:01 +0000 (14:24 +0000)
committerrac61@labyrinth.cl.cam.ac.uk <rac61@labyrinth.cl.cam.ac.uk>
Mon, 7 Jul 2003 14:24:01 +0000 (14:24 +0000)
Code cleanups
Save/load state factored out

18 files changed:
tools/control/src/org/xenoserver/cmdline/CommandParser.java
tools/control/src/org/xenoserver/cmdline/ParseDomainDestroy.java
tools/control/src/org/xenoserver/cmdline/ParseDomainList.java
tools/control/src/org/xenoserver/cmdline/ParseDomainNew.java
tools/control/src/org/xenoserver/cmdline/ParseDomainStart.java
tools/control/src/org/xenoserver/cmdline/ParseDomainStop.java
tools/control/src/org/xenoserver/cmdline/ParseGroup.java
tools/control/src/org/xenoserver/cmdline/ParseHelp.java
tools/control/src/org/xenoserver/cmdline/ParsePhysicalGrant.java
tools/control/src/org/xenoserver/cmdline/ParsePhysicalList.java
tools/control/src/org/xenoserver/cmdline/ParsePhysicalRevoke.java
tools/control/src/org/xenoserver/control/CommandDomainList.java
tools/control/src/org/xenoserver/control/CommandPhysicalList.java
tools/control/src/org/xenoserver/control/InetAddressPattern.java
tools/control/src/org/xenoserver/control/Library.java
tools/control/src/org/xenoserver/control/Partition.java
tools/control/src/org/xenoserver/control/PartitionManager.java
tools/control/src/org/xenoserver/control/Settings.java

index e5dc21b6582e2940be36183432c0e9988c72a1e9..e04b1cb7d384a66dfe2f9ea667c5c6d8b83efb9b 100644 (file)
@@ -4,9 +4,12 @@ import java.util.Iterator;
 import java.util.LinkedList;
 import java.util.List;
 
-import org.xenoserver.control.Command;
 import org.xenoserver.control.CommandFailedException;
 import org.xenoserver.control.Defaults;
+import org.xenoserver.control.PartitionManager;
+import org.xenoserver.control.Settings;
+import org.xenoserver.control.VirtualDiskManager;
+import org.xenoserver.control.XML;
 
 /**
  * Subclasses of Parser know how to parse arguments for a given command
@@ -84,4 +87,12 @@ public abstract class CommandParser {
     }
     return result;
   }
+
+  protected void loadState() {
+    XML.load_state( PartitionManager.it, VirtualDiskManager.it, Settings.STATE_INPUT_FILE );
+  }
+  
+  protected void saveState() {
+    XML.dump_state( PartitionManager.it, VirtualDiskManager.it, Settings.STATE_OUTPUT_FILE );
+  }
 }
index ffbe3241619d112cd4d682264237984544fb323e..799a9537724fceede93f98583f28fdb48dd56402 100644 (file)
@@ -1,9 +1,7 @@
 package org.xenoserver.cmdline;
 
 import java.util.LinkedList;
-import java.util.List;
 
-import org.xenoserver.control.Command;
 import org.xenoserver.control.CommandDomainDestroy;
 import org.xenoserver.control.CommandFailedException;
 import org.xenoserver.control.Defaults;
index 193d5ac0c8fbe25bf68d2283ac381aa4f9ee0717..bf36252e1d05fcab7d6d9255b58cf0ec66201013 100644 (file)
@@ -1,11 +1,9 @@
 package org.xenoserver.cmdline;
 
 import java.util.LinkedList;
-import java.util.List;
 
-import org.xenoserver.control.Command;
-import org.xenoserver.control.CommandFailedException;
 import org.xenoserver.control.CommandDomainList;
+import org.xenoserver.control.CommandFailedException;
 import org.xenoserver.control.Defaults;
 import org.xenoserver.control.Domain;
 
index bab3778fce44568b69c38bd185dc08a31627135b..66f7ac763f569ee15fb4e7d81bbe579dbbeee4a6 100644 (file)
@@ -1,11 +1,9 @@
 package org.xenoserver.cmdline;
 
 import java.util.LinkedList;
-import java.util.List;
 
-import org.xenoserver.control.Command;
-import org.xenoserver.control.CommandFailedException;
 import org.xenoserver.control.CommandDomainNew;
+import org.xenoserver.control.CommandFailedException;
 import org.xenoserver.control.Defaults;
 
 public class ParseDomainNew extends CommandParser {
index b615907d1e196e9e38823700f7c13ce9df4fd492..826057188f1762055a99502e29f9a82e283637de 100644 (file)
@@ -1,11 +1,9 @@
 package org.xenoserver.cmdline;
 
 import java.util.LinkedList;
-import java.util.List;
 
-import org.xenoserver.control.Command;
-import org.xenoserver.control.CommandFailedException;
 import org.xenoserver.control.CommandDomainStart;
+import org.xenoserver.control.CommandFailedException;
 import org.xenoserver.control.Defaults;
 
 public class ParseDomainStart extends CommandParser {
index 40bc152f5899c23331c69f0c99d89645a3183241..c2a9f1093be2124f8c62bcacefac15f397014c5a 100644 (file)
@@ -1,11 +1,9 @@
 package org.xenoserver.cmdline;
 
 import java.util.LinkedList;
-import java.util.List;
 
-import org.xenoserver.control.Command;
-import org.xenoserver.control.CommandFailedException;
 import org.xenoserver.control.CommandDomainStop;
+import org.xenoserver.control.CommandFailedException;
 import org.xenoserver.control.Defaults;
 
 public class ParseDomainStop extends CommandParser {
index 02ef3743d3dfbfa172d9e8abe8e5408f7799d228..cb21eefd8f3f97a2ceea9b0612b3263007a067e3 100644 (file)
@@ -1,8 +1,6 @@
 package org.xenoserver.cmdline;
 
-import java.util.Arrays;
 import java.util.LinkedList;
-import java.util.List;
 
 import org.xenoserver.control.CommandFailedException;
 import org.xenoserver.control.Defaults;
index 4d204c8ba4399b930f8c9d604ffa01ffddf9f463..a9a7138e850d6b075a16842251b79d7c99fc8b0e 100644 (file)
@@ -1,9 +1,7 @@
 package org.xenoserver.cmdline;
 
 import java.util.LinkedList;
-import java.util.List;
 
-import org.xenoserver.control.Command;
 import org.xenoserver.control.Defaults;
 
 public class ParseHelp extends CommandParser {
index ba8198327397ab3a991c967d6ca5459e3441a63c..9979d6e4f0cc1bdf464da624332e4065d39e0026 100644 (file)
@@ -9,8 +9,6 @@ import org.xenoserver.control.Extent;
 import org.xenoserver.control.Mode;
 import org.xenoserver.control.Partition;
 import org.xenoserver.control.PartitionManager;
-import org.xenoserver.control.Settings;
-import org.xenoserver.control.XML;
 
 public class ParsePhysicalGrant extends CommandParser {
   public void parse(Defaults d, LinkedList args) throws ParseFailedException, CommandFailedException {
@@ -31,7 +29,7 @@ public class ParsePhysicalGrant extends CommandParser {
       mode = Mode.READ_ONLY;
       
     // Initialise the partition manager and look up the partition
-    XML.load_state( PartitionManager.it, Settings.STATE_INPUT_FILE );
+    loadState();
     Partition p = PartitionManager.it.get_partition(partition_name);
     
     if ( p == null )
index c08a881a26eee41d196308c84225d9f63fd52905..84d849a28c994b5c8cfab58b06e0e7a32767dd78 100644 (file)
@@ -9,11 +9,8 @@ import org.xenoserver.control.CommandPhysicalList;
 import org.xenoserver.control.Defaults;
 import org.xenoserver.control.Extent;
 import org.xenoserver.control.Library;
-import org.xenoserver.control.Mode;
 import org.xenoserver.control.Partition;
 import org.xenoserver.control.PartitionManager;
-import org.xenoserver.control.Settings;
-import org.xenoserver.control.XML;
 
 public class ParsePhysicalList extends CommandParser {
 
@@ -23,7 +20,7 @@ public class ParsePhysicalList extends CommandParser {
       throw new ParseFailedException("Expected -n<domain_id>");
 
     // Initialise the partition manager
-    XML.load_state( PartitionManager.it, Settings.STATE_INPUT_FILE );
+    loadState();
     
     CommandPhysicalList list = new CommandPhysicalList( d, domain_id );
     String output = list.execute();
index 8527845cdbe37f6bf16bd8d9ace371155c45b128..9db83a5b85f963e434bc3972e617632e22f1036a 100644 (file)
@@ -8,8 +8,6 @@ import org.xenoserver.control.Defaults;
 import org.xenoserver.control.Extent;
 import org.xenoserver.control.Partition;
 import org.xenoserver.control.PartitionManager;
-import org.xenoserver.control.Settings;
-import org.xenoserver.control.XML;
 
 public class ParsePhysicalRevoke extends CommandParser {
   public void parse(Defaults d, LinkedList args) throws ParseFailedException, CommandFailedException {
@@ -22,7 +20,7 @@ public class ParsePhysicalRevoke extends CommandParser {
       throw new ParseFailedException("Expected -p<partition_name>");
       
     // Initialise the partition manager and look up the partition
-    XML.load_state( PartitionManager.it, Settings.STATE_INPUT_FILE );
+    loadState();
     Partition p = PartitionManager.it.get_partition(partition_name);
     
     if ( p == null )
index 39ccd383e1649c86f8b6d32f75749ab884d4b832..69b2ca04a94b0626fd6673384b858c1fe45c258d 100644 (file)
@@ -27,7 +27,6 @@ public class CommandDomainList extends Command {
    */
   public String execute() throws CommandFailedException {
     Runtime r = Runtime.getRuntime();
-    int rc = 0;
     Vector v = new Vector();
     String outline;
     BufferedReader in;
index 03bed9e15d9bb3c7f5d9c33a226e3db48eba962e..abaf4a7213d609adeed1135ea9e6e1696697d4c2 100644 (file)
@@ -31,7 +31,6 @@ public class CommandPhysicalList extends Command {
    */
   public String execute() throws CommandFailedException {
     Runtime r = Runtime.getRuntime();
-    int rc = 0;
     String outline;
     BufferedReader in;
     String output = null;
index 7b45b093f8a53b35ba8f49c8655ab9698ce8187a..79c2385ef854962d94b649a841a8dd99012bbc49 100644 (file)
@@ -1,6 +1,7 @@
 package org.xenoserver.control;
 
-import java.net.*;
+import java.net.InetAddress;
+import java.net.UnknownHostException;
 
 public class InetAddressPattern
 {
@@ -11,7 +12,6 @@ public class InetAddressPattern
   {
     InetAddressPattern result = new InetAddressPattern ();
     char[] ca = t.toCharArray ();
-    int idx = 0;
     int len = ca.length;
 
     try {
index b384bed19b72d53bfd44056f62142acd28c857b4..d79e01aef50d3ab427a1b3644e6ee6f9bb77e49f 100644 (file)
@@ -126,4 +126,41 @@ Library
       return null;
     }
   }
+  
+  /**
+   * Formats a number of bytes in whichever way makes most sense based
+   * on magnitude and width.
+   * 
+   * @param size Number of bytes.
+   * @param width Width of field - at least 5, plz.
+   * @param prefix Set to 1 for left justify
+   * @return The formatted string.
+   */
+  public static String format_size(long size,int width,int prefix) {
+    char[] suffixes = { ' ', 'k', 'M', 'G' };
+    int suffix = 0;
+    long before = size;
+    float after = 0;
+    
+    while ( before > 10000 ) {
+      after = ((float)(before % 1024)) / 1024;
+      before /= 1024;
+      suffix++; 
+    }
+    
+    StringBuffer num = new StringBuffer(width);
+    num.append( Long.toString( before ) );
+    if ( after != 0 ) {
+      int space = width - num.length() - 2;
+      if ( space > 0 ) {
+        num.append( '.' );
+        if ( space > 3 )
+          space = 3;
+        num.append( Integer.toString( (int) (after * Math.pow(10,space))));
+      }
+    }
+    num.append( suffixes[suffix] );
+    
+    return format(num.toString(),width,prefix);
+  }
 }
index 5ca9e02b3e189638d89cde14157ddc837ea332b3..85ea7e372840698e04cd8f5eede763d8bd723241 100644 (file)
@@ -5,7 +5,7 @@
 
 package org.xenoserver.control;
 
-import java.io.*;
+import java.io.PrintWriter;
 
 public class
 Partition
index 2609d458ee69e7d6bdac837da4d2f2bafbf9a3f5..2c936367622a40b6a6ed7e1f09808c8be0f81f28 100644 (file)
@@ -5,10 +5,13 @@
 
 package org.xenoserver.control;
 
-import java.io.*;
+import java.io.BufferedReader;
+import java.io.FileReader;
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.util.Enumeration;
 import java.util.Iterator;
 import java.util.Vector;
-import java.util.Enumeration;
 
 /**
  * PartitionManager manages the partitions on the machine. It is a Singleton
@@ -136,8 +139,6 @@ PartitionManager
   void
   dump_xml (PrintWriter out)
   {
-    int loop;
-
     out.println("<partitions>");
     for (Enumeration e = partition_map.elements() ; e.hasMoreElements() ;) 
     {
index c6a1741ddaf35c0ee3ca41cfb1dbf77c6218080a..a51a7212a6487368e1f63abc153dbce1bf9b33e3 100644 (file)
@@ -19,6 +19,7 @@ public final class Settings
   public static final String PARTITIONS_FILE = System.getProperty("PARTITIONS_FILE", "/proc/partitions");
   public static final String STATE_INPUT_FILE = System.getProperty("STATE_INPUT_FILE", "/var/lib/xen/vdstate.xml");
   public static final String STATE_OUTPUT_FILE = System.getProperty("STATE_OUTPUT_FILE", "/var/lib/xen/vdstate.xml");
+  public static final int SECTOR_SIZE = Integer.parseInt( System.getProperty("SECTOR_SIZE", "512") );
 
   public static File getDefaultsFile() {
     StringTokenizer tok = new StringTokenizer (DEFAULTS_PATH, ":");